package com.cmonbaby.adapter;

import androidx.annotation.Nullable;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.RecyclerView;

import com.cmonbaby.adapter.recycleview.RMultiItemTypeAdapter;
import com.cmonbaby.adapter.recycleview.wrapper.EmptyWrapper;
import com.cmonbaby.adapter.recycleview.wrapper.LoadMoreWrapper;

import java.util.List;

/**
 * 下拉刷新和上拉加载的处理类
 * @author simon 2016-11-04 下午16:05:29
 * @since JDK1.8 SDK6.0.1
 * @version V2.3.4
 */
public class RViewHelper {

    private SwipeRefreshLayout swipeRefresh;
    private SwipeRefreshHelper swipeRefreshHelper; // 下拉刷新的工具类
    private RecyclerView recyclerView;
    private RMultiItemTypeAdapter adapter;
    private RecyclerView.LayoutManager layoutManager;
    private RecyclerView.ItemDecoration itemDecoration;
    private int emptyView;
    private int loadMoreView;
    private int currentPageNum; // 当前页数
    private int startPageNumber; // 开始页数。因为页数可能不是从1开始的。
    private int rowsPageNumber; // 最后一页少于多少条数据显示无更多数据
    private boolean isSupportPaging;
    private LoadMoreWrapper loadMoreWrapper;
    private EmptyWrapper emptyWrapper;
    private OnRecycleViewHelperListener mRecycleViewHelperListener;

    private RViewHelper(Builder builder) {
        this.swipeRefresh = builder.swipeRefresh;
        this.recyclerView = builder.recyclerView;
        this.adapter = builder.adapter;
        this.layoutManager = builder.layoutManager;
        this.itemDecoration = builder.itemDecoration;
        this.emptyView = builder.emptyView;
        this.loadMoreView = builder.loadMoreView;
        this.startPageNumber = builder.startPageNumber;
        this.rowsPageNumber = builder.rowsPageNumber;
        this.isSupportPaging = builder.isSupportPaging;
        this.mRecycleViewHelperListener = builder.recycleViewHelperListener;

        this.currentPageNum = this.startPageNumber;
        int[] colorRes = builder.colorRes;
        if (swipeRefresh != null) {
            if (colorRes == null) {
                swipeRefreshHelper = SwipeRefreshHelper.createSwipeRefreshHelper(swipeRefresh);
            } else {
                swipeRefreshHelper = SwipeRefreshHelper.createSwipeRefreshHelper(swipeRefresh, colorRes);
            }
        }
        init();
    }

    private void init() {
        // recyclerView的初始化与上拉加载更多 */
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        if (itemDecoration != null) recyclerView.addItemDecoration(itemDecoration);

        // 下拉刷新的操作 */
        if (swipeRefreshHelper != null) {
            swipeRefreshHelper.setOnSwipeRefreshHelperListener(() -> { // 下拉刷新，等同于重新加载
                dismissSwipeRefresh();
                currentPageNum = startPageNumber;
                if (mRecycleViewHelperListener != null) mRecycleViewHelperListener.onRefresh();
            });
        }
    }

    public void notifyAdapterDataSetChanged(List data) {
        if (currentPageNum == startPageNumber) { // 首次加载或者下拉刷新重新加载
            adapter.updateDatas(data);
        } else {
            adapter.addDatas(data);
            getLoadMoreWrapper().notifyDataSetChanged();
            return;
        }

        // 加载更多的操作 */
        if (isSupportPaging) {
            loadMoreWrapper = new LoadMoreWrapper(adapter, rowsPageNumber);
            loadMoreWrapper.setLoadMoreView(loadMoreView);
            recyclerView.setAdapter(loadMoreWrapper);
            loadMoreWrapper.setOnLoadMoreListener(new LoadMoreWrapper.OnLoadMoreListener() {
                @Override
                public void onLoadMoreRequested() {
                    currentPageNum++; // 加载更多数据，只改变了pageNum变量，其他相同
                    if (mRecycleViewHelperListener != null) mRecycleViewHelperListener.onLoadMore();
                }

                @Override
                public void onNoMoreData() {
                    if (mRecycleViewHelperListener != null) mRecycleViewHelperListener.onNoMoreData();
                }
            });
        } else {
            recyclerView.setAdapter(adapter);
        }

        // 空数据的操作 */
        if (emptyView != 0) {
            if (data == null || data.size() == 0) {
                emptyWrapper = new EmptyWrapper(adapter);
                emptyWrapper.setEmptyView(emptyView);
                recyclerView.setAdapter(emptyWrapper);
            }
        }
    }

    //---------------------------提供对外暴露自己属性的方法-------------------------
    public void setCurrentPageNum(int currentPageNum) {
        this.currentPageNum = currentPageNum;
    }

    public void resetAdapter() {
        this.currentPageNum = this.startPageNumber;
        this.adapter.clearAdapterDatas();
    }

    /** 获取当前页码 */
    public int getCurrentPageNum() {
        return currentPageNum;
    }

    /** 获取RecyclerView */
    public RecyclerView getRecyclerView() {
        return recyclerView;
    }

    /** 获取适配器 */
    public RMultiItemTypeAdapter getAdapter() {
        return adapter;
    }

    /** 获取加载更多布局 */
    public LoadMoreWrapper getLoadMoreWrapper() { return loadMoreWrapper; }

    /** 获取空数据布局 */
    public EmptyWrapper getEmptyWrapper() { return emptyWrapper; }

    /** 显示SwipeRefresh */
    public void showSwipeRefresh() {
        if (swipeRefresh != null && !swipeRefresh.isRefreshing()) {
            swipeRefresh.setRefreshing(true);
        }
    }

    /** 隐藏SwipeRefresh */
    public void dismissSwipeRefresh() {
        if (swipeRefresh != null && swipeRefresh.isRefreshing()) {
            swipeRefresh.setRefreshing(false);
        }
    }

    /** 下拉刷新，加载更多，无更多数据接口 */
    public interface OnRecycleViewHelperListener {

        void onRefresh();

        void onLoadMore();

        void onNoMoreData();
    }

    static final class Builder {

        private SwipeRefreshLayout swipeRefresh;
        private int[] colorRes; // 下拉颜色
        private RecyclerView recyclerView;
        private RMultiItemTypeAdapter adapter;
        private RecyclerView.LayoutManager layoutManager;
        private RecyclerView.ItemDecoration itemDecoration;
        private int emptyView;
        private int loadMoreView;
        private int startPageNumber = 1; // 分页开始页数
        private int rowsPageNumber = 10; // 最后一页少于多少条数据显示无更多数据
        private boolean isSupportPaging = true; // 是否支持上拉加载更多
        private OnRecycleViewHelperListener recycleViewHelperListener;

        Builder() {
        }

        Builder swipeRefresh(SwipeRefreshLayout swipeRefresh) {
            this.swipeRefresh = swipeRefresh;
            return this;
        }

        Builder colorRes(int... colorRes) {
            this.colorRes = colorRes;
            return this;
        }

        Builder recycleView(@Nullable RecyclerView recyclerView) {
            this.recyclerView = recyclerView;
            return this;
        }

        Builder recycleViewAdapter(@Nullable RMultiItemTypeAdapter adapter) {
            this.adapter = adapter;
            return this;
        }

        Builder layoutManager(@Nullable RecyclerView.LayoutManager layoutManager) {
            this.layoutManager = layoutManager;
            return this;
        }

        Builder itemDecoration(RecyclerView.ItemDecoration itemDecoration) {
            this.itemDecoration = itemDecoration;
            return this;
        }

        Builder emptyView(int emptyView) {
            this.emptyView = emptyView;
            return this;
        }

        Builder loadMoreView(int loadMoreView) {
            this.loadMoreView = loadMoreView;
            return this;
        }

        Builder rowsPageNumber(int rowsPageNumber) {
            this.rowsPageNumber = rowsPageNumber;
            return this;
        }

        Builder startPageNumber(int startPageNumber) {
            this.startPageNumber = startPageNumber;
            return this;
        }

        Builder isSupportPaging(boolean isSupportPaging) {
            this.isSupportPaging = isSupportPaging;
            return this;
        }

        Builder recycleViewHelperListener(OnRecycleViewHelperListener recycleViewHelperListener) {
            this.recycleViewHelperListener = recycleViewHelperListener;
            return this;
        }

        RViewHelper build() {
            return new RViewHelper(this);
        }
    }
}